package com.jianzh5.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.dao.BaseDaoI;
import com.jianzh5.entity.TCategory;
import com.jianzh5.entity.TNews;
import com.jianzh5.entity.TProduct;
import com.jianzh5.page.PageUtil;
import com.jianzh5.page.PageView;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.pojo.PProduct;
import com.jianzh5.service.NewsServiceI;
import com.jianzh5.service.ProductServiceI;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jianzh5 on 2016/11/24.
 */
@Service("newsService")
public class NewsServiceImpl implements NewsServiceI{

    private final Logger logger = Logger.getLogger(NewsServiceImpl.class);

    @Autowired
    private BaseDaoI<TNews> newsDao;

    @Autowired
    private BaseDaoI<TProduct> productDao;

    @Autowired
    private BaseDaoI<TCategory> categoryDao;

    @Override
    public DataTables dataTables(String order, String sort, DataPage dataPage) {
        String hql = "from TNews t ";
        String totalHql = "select count(*) from TNews t";
        hql += " order by t."+sort+" "+order;
        List<TNews> tlist = newsDao.findByPosition(hql, dataPage.getStart(), dataPage.getLength());
        long records = productDao.count(totalHql);
        DataTables dt = new DataTables();
        dt.setDraw(dataPage.getDraw());
        dt.setRecordsTotal(records);
        dt.setRecordsFiltered(records);
        dt.setData(tlist);
        return dt;
    }

    @Override public TNews getNews(Integer id) {
        return newsDao.get(TNews.class,id);
    }

    @Override
    public void updateNews(TNews news, PAdmin pAdmin) {
        if(news.getId() == null  || news.getId() == 0){  //添加
            news.setCreateDate(new Date());
            news.setCreateUser(pAdmin.getUsername());
            newsDao.save(news);
        }else{
            TNews t = newsDao.get(TNews.class,news.getId());
            BeanUtils.copyProperties(news,t,new String[]{"createDate","createUser"});
        }
    }

    @Override
    public void delNews(int id) {
        TNews t = newsDao.get(TNews.class,id);
        newsDao.delete(t);

    }


    /**
     * 首页获取指定的查询数据
     * @param page
     * @param type 新闻类别
     * @return
     */
    @Override
    public PageView<TNews> newsList(PageView<TNews> page,Integer type,String sort) {
        if(sort == null || sort.equals("")){
            sort = "createDate desc";
        }
        String hql = "from TNews t where 1 = 1";
        String totalHql = "select count(*) from TNews t where 1 = 1";
        Map<String,Object> params = Maps.newHashMap();
        if(type != null){
            params.put("type",type);
            hql += " and t.type = :type";
            totalHql += " and t.type = :type";
        }
        hql += " order by t."+sort;
        int total = (int)newsDao.count(totalHql,params);
        page = PageUtil.createPage(page,total);
        List<TNews> tlist = newsDao.find(hql, params,page.getCpage(), page.getSize());
        page.setRecords(tlist);
        return page;
    }

    @Override
    public List<TNews> getNewsList(int start,int end,String sort) {
        String hql = "from TNews t order by t."+sort;
        List<TNews> tList = newsDao.findByPosition(hql, start, end);
        return tList;
    }


}
